/*
 * ============================================================================
 *                   GNU Lesser General Public License
 * ============================================================================
 *
 *
 *
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 *
 *
 *
 */
package rad.framework.search.directory;
import java.io.IOException;

import org.apache.commons.lang.StringUtils;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Unwrap;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
import org.jboss.seam.log.Log;

/**
 * Manage the creation of a directory store. This based implementation uses a
 * file store.
 * 
 * @author boudyacho
 */
@Name("rad.framework.search.directory")
@Scope(ScopeType.EVENT)
@Install(value = true, precedence = Install.FRAMEWORK)
@AutoCreate
@BypassInterceptors
public class ManagedDirectory {

	@Logger
	private Log log;

	private FSDirectory directory;

	private String indexBase;
	
	private String indexName = "rad-framework";
	
	/**
	 * Returns the indexBase if it's intialized, otherwise it sets indexBase to 
	 * hibernate.search.default.indexBase 
	 * 
	 * @return indexBase
	 * */
	public String getIndexBase() {
		if (StringUtils.isEmpty(indexBase)) {
			indexBase = System.getProperty("hibernate.search.default.indexBase", ".");
		}
		return indexBase;
	}
	
	/**
	 * Sets indexBase to the parameter's value. The indexBase is the directory where
	 * indexName directory's are held.
	 * 
	 * @param indexBase new indexBase name*/
	public void setIndexBase(String indexBase) {
		this.indexBase = indexBase;
	}
	
	/**
	 * Gets the indexName. The indexName is the name of the folder where the indices will
	 * be stored.
	 * 
	 * @return the current indexName
	 * */
	public String getIndexName() {
		return indexName;
	}
	
	/**
	 * Sets the indexName to the value in the parameter. 
	 * 
	 * @param indexName The name of the folder where the indices will be held. 
	 * */
	public void setIndexName(String indexName) {
		this.indexName = indexName;
	}

	@Create
	/**
	 * Auto creates the ManagedDirectory singleton instance on startup based on the values
	 * of indexBase and indexName.
	 * */
	public void create() throws IOException {
		log.debug("Creating: #0", this);
		directory = FSDirectory.getDirectory(getIndexBase() + "/" + getIndexName());
	}

	@Unwrap
	/**
	 * Returns the defined directory created on startup.
	 * 
	 * @return the stored Directory variable.
	 *  */
	public Directory getDirectory() {
		return directory;
	}
	
	/**
	 * @return the singleton instance of the directory.*/
	public static Directory instance() {
		return (Directory) Component.getInstance(ManagedDirectory.class);
	}

	@Override
	/**
	 * @return a string representation of the directory 
	 * */
	public String toString() {
		return "ManagedDirectory(" + getIndexBase() + "/" + getIndexName() + ")";
	}
}